﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace RoofRunner.Base.Camera
{
    public class BasicCamera : ICamera
    {
        #region Fields
        private float _pitch;
        private float _yaw;
        private Vector3 cameraReference = new Vector3(0, 0, -1);
        #endregion // Fields


        #region Properties
        public Matrix View { get; set; }
        public Matrix Projection { get; set; }


        public Vector3 Position { get; set; }
        public Vector3 Target { get; set; }

        public float Yaw
        {
            get { return _yaw; }
            set { _yaw = value % 360; }
        }
        public float Pitch
        {
            get { return _pitch; }
            set { _pitch = MathHelper.Clamp(value, -89.9f, 89.9f); }
        }
        #endregion // Properties


        #region Constructors
        public BasicCamera()
        {
            this.Position = new Vector3(2, 3, -5);
            this.Target = Vector3.Zero;

            UpdateCamera();
        }
        #endregion // Constructors


        #region Methods
        public void Update(GameTime gameTime)
        {
            UpdateCamera();
        }

        protected void UpdateCamera()
        {
            //Matrix rotMatrix = Matrix.CreateRotationY(AvatarYaw);
            float yaw = MathHelper.ToRadians(this.Yaw);
            float pitch = MathHelper.ToRadians(this.Pitch);
            float roll = 0.0f;

            Matrix rotMatrix = Matrix.CreateFromYawPitchRoll(yaw, pitch, roll);
            Vector3 transRef = Vector3.Transform(cameraReference, rotMatrix);
            this.Target = this.Position + transRef;

            View = Matrix.CreateLookAt(this.Position, this.Target, Vector3.Up);
        }
        #endregion // Methods
    }
}
